﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace lcmyjobs.DAL
{
	/// <summary>
	/// 数据访问类:adverts
	/// </summary>
	public partial class adverts
	{
		public adverts()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("aid", "adverts"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int aid)
		{
			int rowsAffected;
			SqlParameter[] parameters = {
					new SqlParameter("@aid", SqlDbType.Int,4)
};
			parameters[0].Value = aid;

			int result= DbHelperSQL.RunProcedure("adverts_Exists",parameters,out rowsAffected);
			if(result==1)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		///  增加一条数据
		/// </summary>
		public int Add(lcmyjobs.Model.adverts model)
		{
			int rowsAffected;
			SqlParameter[] parameters = {
					new SqlParameter("@aid", SqlDbType.Int,4),
                    
					new SqlParameter("@pics", SqlDbType.VarChar,50),
					new SqlParameter("@alink", SqlDbType.VarChar,50),
					new SqlParameter("@locationid", SqlDbType.Int,4),
                                        new SqlParameter("@companyname", SqlDbType.VarChar,50)};
			parameters[0].Direction = ParameterDirection.Output;
			parameters[1].Value = model.pics;
			parameters[2].Value = model.alink;
			parameters[3].Value = model.locationid;
            parameters[4].Value = model.companyname;

			DbHelperSQL.RunProcedure("adverts_ADD",parameters,out rowsAffected);
			return (int)parameters[0].Value;
		}

        /// <summary>
		///  增加一个广告图片
		/// </summary>
        public int Addpic(lcmyjobs.Model.adverts model)
        {
            int rowsAffected;
            SqlParameter[] parameters = {
					new SqlParameter("@aid", SqlDbType.Int,4),
					new SqlParameter("@pics", SqlDbType.VarChar,50),
					new SqlParameter("@alink", SqlDbType.VarChar,50),
					new SqlParameter("@ispicdisplay", SqlDbType.Int,4),
                    new SqlParameter("@companyname", SqlDbType.VarChar,50),
                    new SqlParameter("@apid", SqlDbType.Int,4),
                    new SqlParameter("@res", SqlDbType.Int,4),
            };

            parameters[0].Value = model.aid;
            parameters[1].Value = model.pics;
            parameters[2].Value = model.alink;
            parameters[3].Value = model.ispicdisplay;
            parameters[4].Value = model.companyname;
            parameters[5].Value = model.apid;
            parameters[6].Value = ParameterDirection.Output;


            DbHelperSQL.RunProcedure("adverts_Addpic", parameters, out rowsAffected);
            return rowsAffected;
        }

		/// <summary>
		///  更新一条数据
		/// </summary>
		public bool Update(lcmyjobs.Model.adverts model)
		{
			int rowsAffected=0;
			SqlParameter[] parameters = {
					new SqlParameter("@aid", SqlDbType.Int,4),
					new SqlParameter("@pics", SqlDbType.VarChar,50),
					new SqlParameter("@alink", SqlDbType.VarChar,50),
					new SqlParameter("@locationid", SqlDbType.Int,4),
                                        new SqlParameter("@companyname", SqlDbType.VarChar,50)};
			parameters[0].Value = model.aid;
			parameters[1].Value = model.pics;
			parameters[2].Value = model.alink;
			parameters[3].Value = model.locationid;
            parameters[4].Value = model.companyname;
			DbHelperSQL.RunProcedure("adverts_Update",parameters,out rowsAffected);
			if (rowsAffected > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

        
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public int Updatepic(int aid, int apid)
        {
            string sql = "update advpics set ispicdisplay = 0 where aid = " + aid.ToString();
            sql += " update advpics set ispicdisplay = 1 where apid = " + apid.ToString();
            return DbHelperSQL.ExecuteSql(sql);

        }

        public int Updateadv(string aid,string strset)
        {
            if (strset != null || strset != "")
            {
                string sql = "update adverts set " + strset;
                sql += " where aid = " + aid;
                return DbHelperSQL.ExecuteSql(sql);
            }
            else
            {
                return 0;
            }
        }


		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int aid)
		{
			int rowsAffected=0;
			SqlParameter[] parameters = {
					new SqlParameter("@aid", SqlDbType.Int,4)
};
			parameters[0].Value = aid;

			DbHelperSQL.RunProcedure("adverts_Delete",parameters,out rowsAffected);
			if (rowsAffected > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string aidlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from adverts ");
			strSql.Append(" where aid in ("+aidlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public lcmyjobs.Model.adverts GetModel(int aid)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@aid", SqlDbType.Int,4)
};
			parameters[0].Value = aid;

			lcmyjobs.Model.adverts model=new lcmyjobs.Model.adverts();
			DataSet ds= DbHelperSQL.RunProcedure("adverts_GetModel",parameters,"ds");
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["aid"]!=null && ds.Tables[0].Rows[0]["aid"].ToString()!="")
				{
					model.aid=int.Parse(ds.Tables[0].Rows[0]["aid"].ToString());
				}
				if(ds.Tables[0].Rows[0]["pics"]!=null && ds.Tables[0].Rows[0]["pics"].ToString()!="")
				{
					model.pics=ds.Tables[0].Rows[0]["pics"].ToString();
				}
				if(ds.Tables[0].Rows[0]["alink"]!=null && ds.Tables[0].Rows[0]["alink"].ToString()!="")
				{
					model.alink=ds.Tables[0].Rows[0]["alink"].ToString();
				}
				if(ds.Tables[0].Rows[0]["locationid"]!=null && ds.Tables[0].Rows[0]["locationid"].ToString()!="")
				{
					model.locationid=int.Parse(ds.Tables[0].Rows[0]["locationid"].ToString());
				}
                if (ds.Tables[0].Rows[0]["companyname"] != null && ds.Tables[0].Rows[0]["companyname"].ToString() != "")
                {
                    model.companyname = ds.Tables[0].Rows[0]["companyname"].ToString();
                }
				return model;
			}
			else
			{
				return null;
			}
		}
        public DataSet GetAdvnid(int uid)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@locationid", SqlDbType.Int,4)
};
            parameters[0].Value = uid;
            DataSet ds = DbHelperSQL.RunProcedure("[dbo].[getadvertsbynid]", parameters, "ds");
            return ds;
        }
        public DataSet GetAdvnid(int min,int max)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@lmin", SqlDbType.Int,4),
                    new SqlParameter("@lmax", SqlDbType.Int,4)
};
            parameters[0].Value = min;
            parameters[1].Value = max;
            DataSet ds = DbHelperSQL.RunProcedure("[dbo].[getadvertsbynids]", parameters, "ds");
            return ds;
        }
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
            strSql.Append("select aid,pics,alink,locationid,companyname,islocdisplay,apid,ispicdisplay,strdisplay=(case ispicdisplay when 1 then '显示' else '选择' end) ");
			strSql.Append(" FROM View_advpics ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
            strSql.Append(" order by locationid");
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
            strSql.Append(" aid,pics,alink,locationid,companyname ");
			strSql.Append(" FROM adverts ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}
        public DataSet Getpic(int apid)
        {
            string sql = "select companyname,pics,alink,ispicdisplay from advpics where apid = " + apid.ToString();
            return DbHelperSQL.Query(sql);
        }
		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "adverts";
			parameters[1].Value = "aid";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  Method
	}
}

